<template>
    <view class="assessment-detail-container">
        <!-- 评估信息 -->
        <view class="assessment-info">
            <view class="header">
                <view class="icon" :style="{ backgroundColor: assessment.color }">
                    <u-icon :name="assessment.icon" size="40" color="#fff"></u-icon>
                </view>
                <view class="title">{{assessment.title}}</view>
            </view>
            <view class="description">{{assessment.description}}</view>
            <view class="meta">
                <view class="item">
                    <text class="label">题目数量</text>
                    <text class="value">{{assessment.questionCount}}题</text>
                </view>
                <view class="item">
                    <text class="label">预计耗时</text>
                    <text class="value">{{assessment.estimatedTime}}分钟</text>
                </view>
            </view>
        </view>
        
        <!-- 评估说明 -->
        <view class="instruction-section">
            <view class="section-title">评估说明</view>
            <view class="content">
                <view class="item" v-for="(item, index) in assessment.instructions" :key="index">
                    <text class="number">{{index + 1}}.</text>
                    <text class="text">{{item}}</text>
                </view>
            </view>
        </view>
        
        <!-- 评估内容 -->
        <view class="question-section">
            <view class="section-title">评估内容</view>
            <view class="questions">
                <view class="question-item" v-for="(question, index) in assessment.questions" :key="index">
                    <view class="question-header">
                        <text class="number">{{index + 1}}.</text>
                        <text class="text">{{question.text}}</text>
                    </view>
                    <view class="options">
                        <view 
                            class="option" 
                            v-for="(option, optionIndex) in question.options" 
                            :key="optionIndex"
                            :class="{ selected: selectedAnswers[index] === optionIndex }"
                            @click="selectAnswer(index, optionIndex)"
                        >
                            <text class="option-text">{{option.text}}</text>
                            <text class="option-score" v-if="option.score">({{option.score}}分)</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        
        <!-- 底部按钮 -->
        <view class="bottom-buttons">
            <u-button type="primary" @click="submitAssessment">提交评估</u-button>
        </view>
    </view>
</template>

<script>
import request from '@/util/request'

export default {
    data() {
        return {
            assessment: {
                id: 1,
                title: '脑卒中复发风险评估',
                description: '通过评估您的健康状况和治疗情况，预测脑卒中复发的风险。',
                icon: 'warning',
                color: '#FF6B6B',
                questionCount: 12,
                estimatedTime: 6,
                instructions: [
                    '请根据您的实际情况如实回答以下问题',
                    '每个问题只能选择一个答案',
                    '评估结果仅供参考，不能替代专业医疗建议',
                    '建议定期进行风险评估，及时了解复发风险'
                ],
                questions: [
                    {
                        text: '您上次脑卒中发作距今多久？',
                        options: [
                            { text: '3个月内', score: 3 },
                            { text: '3-6个月', score: 2 },
                            { text: '6-12个月', score: 1 },
                            { text: '1年以上', score: 0 }
                        ]
                    },
                    {
                        text: '您是否按时服用抗血小板药物？',
                        options: [
                            { text: '是，规律服用', score: 0 },
                            { text: '偶尔漏服', score: 1 },
                            { text: '经常漏服', score: 2 },
                            { text: '未服用', score: 3 }
                        ]
                    },
                    {
                        text: '您的血压控制情况如何？',
                        options: [
                            { text: '血压达标，稳定', score: 0 },
                            { text: '血压偶尔偏高', score: 1 },
                            { text: '血压经常偏高', score: 2 },
                            { text: '血压未控制', score: 3 }
                        ]
                    }
                ]
            },
            selectedAnswers: []
        }
    },
    methods: {
        async loadAssessmentDetail() {
            try {
                const id = this.$route.query.id
                const res = await request.get(`/api/assessment/risk/detail/${id}`)
                if (res.code === 200) {
                    this.assessment = res.data
                    this.selectedAnswers = new Array(res.data.questions.length).fill(-1)
                }
            } catch (error) {
                console.log(error)
            }
        },
        
        selectAnswer(questionIndex, optionIndex) {
            this.$set(this.selectedAnswers, questionIndex, optionIndex)
        },
        
        async submitAssessment() {
            // 检查是否所有问题都已回答
            if (this.selectedAnswers.includes(-1)) {
                uni.showToast({
                    title: '请回答所有问题',
                    icon: 'none'
                })
                return
            }
            
            try {
                const res = await request.post('/api/assessment/risk/submit', {
                    assessmentId: this.assessment.id,
                    answers: this.selectedAnswers
                })
                
                if (res.code === 200) {
                    uni.navigateTo({
                        url: `/pages/assessment/result?id=${res.data.id}`
                    })
                }
            } catch (error) {
                console.log(error)
            }
        }
    },
    onLoad() {
        this.loadAssessmentDetail()
    }
}
</script>

<style lang="less" scoped>
.assessment-detail-container {
    min-height: 100vh;
    background-color: #F8F8F8;
    padding-bottom: 120rpx;
    
    .assessment-info {
        background-color: #fff;
        padding: 40rpx 30rpx;
        
        .header {
            display: flex;
            align-items: center;
            margin-bottom: 20rpx;
            
            .icon {
                width: 80rpx;
                height: 80rpx;
                border-radius: 40rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                margin-right: 20rpx;
            }
            
            .title {
                font-size: 36rpx;
                font-weight: bold;
                color: #333;
            }
        }
        
        .description {
            font-size: 28rpx;
            color: #666;
            line-height: 1.6;
            margin-bottom: 30rpx;
        }
        
        .meta {
            display: flex;
            
            .item {
                flex: 1;
                display: flex;
                flex-direction: column;
                align-items: center;
                
                .label {
                    font-size: 24rpx;
                    color: #999;
                    margin-bottom: 6rpx;
                }
                
                .value {
                    font-size: 32rpx;
                    color: #333;
                    font-weight: bold;
                }
            }
        }
    }
    
    .instruction-section,
    .question-section {
        background-color: #fff;
        margin-top: 20rpx;
        padding: 30rpx;
        
        .section-title {
            font-size: 32rpx;
            font-weight: bold;
            color: #333;
            margin-bottom: 20rpx;
        }
    }
    
    .instruction-section {
        .content {
            .item {
                display: flex;
                margin-bottom: 20rpx;
                
                &:last-child {
                    margin-bottom: 0;
                }
                
                .number {
                    font-size: 28rpx;
                    color: #333;
                    font-weight: bold;
                    margin-right: 10rpx;
                }
                
                .text {
                    flex: 1;
                    font-size: 28rpx;
                    color: #666;
                    line-height: 1.6;
                }
            }
        }
    }
    
    .question-section {
        .questions {
            .question-item {
                margin-bottom: 40rpx;
                
                &:last-child {
                    margin-bottom: 0;
                }
                
                .question-header {
                    display: flex;
                    margin-bottom: 20rpx;
                    
                    .number {
                        font-size: 28rpx;
                        color: #333;
                        font-weight: bold;
                        margin-right: 10rpx;
                    }
                    
                    .text {
                        flex: 1;
                        font-size: 28rpx;
                        color: #333;
                        line-height: 1.6;
                    }
                }
                
                .options {
                    .option {
                        display: flex;
                        align-items: center;
                        padding: 20rpx;
                        background-color: #F8F8F8;
                        border-radius: 10rpx;
                        margin-bottom: 10rpx;
                        
                        &:last-child {
                            margin-bottom: 0;
                        }
                        
                        &.selected {
                            background-color: #E1F3D8;
                        }
                        
                        .option-text {
                            flex: 1;
                            font-size: 28rpx;
                            color: #333;
                        }
                        
                        .option-score {
                            font-size: 24rpx;
                            color: #999;
                            margin-left: 10rpx;
                        }
                    }
                }
            }
        }
    }
    
    .bottom-buttons {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #fff;
        padding: 20rpx 30rpx;
        box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
    }
}
</style> 